//
//  HTVistorView.swift
//  mySinaBlog
//
//  Created by whtapple on 3/22/16.
//  Copyright © 2016 whtapple. All rights reserved.
//

import UIKit
@objc protocol HTVistorViewDelegate: NSObjectProtocol{
    optional func clickRegisterButton()
    optional func clickLoginButton()
}


class HTVistorView: UIView {
    weak var delegate: HTVistorViewDelegate?
    //首页图片转动动画
    func rotate(){
        let rotate = CABasicAnimation(keyPath: "transform.rotation")
        rotate.toValue = M_PI * 2
        rotate.duration = 20
        rotate.repeatCount = MAXFLOAT
        rotate.removedOnCompletion = false
        iconView.layer.addAnimation(rotate, forKey: nil)
    }
    //设置每个 View的信息
    func setupVisitorView(imageName: String, message: String){
        iconView.image = UIImage(named: imageName)
        messageLabel.text = message
        houseView.hidden = true
        self.sendSubviewToBack(coverView)
    }
    //初始化加载控件
    override init(frame: CGRect) {
        super.init(frame:frame)
        self.backgroundColor = UIColor(white: 237.0/255.0, alpha: 1)
        prepareUI()
        
    }
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    //准备 UI 方法
    private func prepareUI() {
        //添加子控件
        addSubview(iconView)
        addSubview(coverView)
        addSubview(houseView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        // 添加约束
        iconView.translatesAutoresizingMaskIntoConstraints = false;
        coverView.translatesAutoresizingMaskIntoConstraints = false;
        houseView.translatesAutoresizingMaskIntoConstraints = false;
        messageLabel.translatesAutoresizingMaskIntoConstraints = false;
        registerButton.translatesAutoresizingMaskIntoConstraints = false;
        loginButton.translatesAutoresizingMaskIntoConstraints = false;
        //加约束
        //iconView约束
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
         addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: -100))
        //houseView约束
        addConstraint(NSLayoutConstraint(item: houseView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: houseView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: -100))
        //messageLabel约束
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 250))
        //registerButton约束
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: -30))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel , attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 60))
          addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 30))
        //loginButton约束
         addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 30))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: registerButton, attribute: NSLayoutAttribute.Top, multiplier: 1.0, constant: 0))
         addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: registerButton, attribute: NSLayoutAttribute.Width, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: registerButton, attribute: NSLayoutAttribute.Height, multiplier: 1.0, constant: 0))
        //遮罩约束
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Leading, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Trailing, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: loginButton, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 0))
    }
   // MARK: - 懒加载
    //转轮
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //遮罩
    private lazy var coverView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    //房子图片
    private lazy var houseView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //文字 label
    lazy var messageLabel: UILabel = {
        let label = UILabel()
        label.text = "关注一些人，看看有什么惊喜"
        label.textAlignment = NSTextAlignment.Center
        label.numberOfLines = 0
        label.font = UIFont.systemFontOfSize(18)
        return label
    }()
    //注册按钮
    private lazy var registerButton: UIButton = {
        let btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.setTitle("注册", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        btn.addTarget(self, action: "didClickRegisterButton", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    }()
    //登陆按钮
    private lazy var loginButton: UIButton = {
        let btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.setTitle("登陆", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        btn.addTarget(self, action: "didClickLoginButton", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    }()
    //完成注册按钮点击调用
   func didClickRegisterButton(){
        delegate?.clickRegisterButton?()
    }
    //完成登陆按钮调用
   func didClickLoginButton(){
        delegate?.clickLoginButton?()
    }
}
